package com.firefightingiot.firefightingiot.config.scheduling;

import com.firefightingiot.firefightingiot.model.statistics.DeviceNormalLogElectricEnergy;
import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.web.mapper.DeviceNormalLogMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class NormalLogScheduling {

    @Resource
    public MyLogger logger;

    @Resource
    DeviceNormalLogMapper deviceNormalLogMapper;

    @Scheduled(cron="0 0 22 * * ?")//每晚十点
    public void tableOperation() {
        /**
         * 创建正常数据表
         */
        //创建一个电气火灾正常数据表
        StringBuffer sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_electric_fire_" + DayUtil.getTomorrow() + "` like `device_normal_log_electric_fire_" + DayUtil.getToday() + "`");
        String createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个建筑消防用水正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_fire_water_" + DayUtil.getTomorrow() + "` like `device_normal_log_fire_water_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个可燃气体正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_gas_" + DayUtil.getTomorrow() + "` like `device_normal_log_gas_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个灭火器正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_extinguisher_" + DayUtil.getTomorrow() + "` like `device_normal_log_extinguisher_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个烟感正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_smoke_" + DayUtil.getTomorrow() + "` like `device_normal_log_smoke_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个水泵正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_water_pump_" + DayUtil.getTomorrow() + "` like `device_normal_log_water_pump_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个温湿度正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_temperature_humidity_" + DayUtil.getTomorrow() + "` like `device_normal_log_temperature_humidity_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        //创建一个充电桩正常数据表
        sb = new StringBuffer();
        sb.append("CREATE TABLE `device_normal_log_charging_station_" + DayUtil.getTomorrow() + "` like `device_normal_log_charging_station_" + DayUtil.getToday() + "`");
        createTableSql = sb.toString();
        deviceNormalLogMapper.definitionTable(createTableSql);
        /**
         * 删除8天前的表
         */
        //删除8天前的电气火灾正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_electric_fire_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的建筑消防用水正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_fire_water_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的可燃气体正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_gas_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的灭火器正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_extinguisher_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的烟感正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_smoke_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的水泵正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_water_pump_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的温湿度正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_temperature_humidity_" + DayUtil.getBeforeEightDay() + "`");
        //删除8天前的充电桩正常数据表
        deviceNormalLogMapper.definitionTable("drop table if exists " + "`device_normal_log_charging_station_" + DayUtil.getBeforeEightDay() + "`");
    }

    /**
     * @Author YangYu
     * @Description 统计电能数据（每天23点执行一次）
     * @Date Created in 16:12 2023/3/29
     */

    @Scheduled(cron="0 0 23 * * ?")
    public void activeElectricAdd(){
        // 电器火灾-电能
        electricEnergy();
        // 充电桩-电能
        chargingStationEnergy();
    }
    // 电器火灾-电能
    public void electricEnergy(){
        String oneTableName = "device_normal_log_electric_fire_"+DayUtil.getToday();
        String twoTableName = "device_normal_log_electric_fire_"+DayUtil.getYestoday().replace("-","");
        //今天数据
        List<Map<String, Object>> todayList = deviceNormalLogMapper.getElectricEnergyList(oneTableName);
        //昨天数据
        List<Map<String, Object>> yesterdayList = deviceNormalLogMapper.getElectricEnergyList(twoTableName);
        //组装电数据
        List<DeviceNormalLogElectricEnergy> electricEnergyList = new ArrayList<>();
        //已添加的设备
        List<String> numberList = new ArrayList<>();
        //时间
        Date date = new Date();
        for (Map<String, Object> todayMap : todayList){
            for (Map<String, Object> yesterdayMap : yesterdayList){
                if (todayMap.get("number").equals(yesterdayMap.get("number"))){
                    double d = Double.parseDouble(todayMap.get("activeElectric").toString())-Double.parseDouble(yesterdayMap.get("activeElectric").toString());
                    DeviceNormalLogElectricEnergy deviceNormalLogElectricEnergy = new DeviceNormalLogElectricEnergy();
                    deviceNormalLogElectricEnergy.setNumber(todayMap.get("number").toString());
                    deviceNormalLogElectricEnergy.setBusinessId(todayMap.get("businessId").toString());
                    deviceNormalLogElectricEnergy.setBusinessName(todayMap.get("businessName").toString());
                    deviceNormalLogElectricEnergy.setActiveElectric(Double.valueOf(String.format("%.2f",d)));
                    deviceNormalLogElectricEnergy.setDateTime(date);
                    electricEnergyList.add(deviceNormalLogElectricEnergy);
                    numberList.add(todayMap.get("number").toString());
                }
            }
            if (!numberList.contains(todayMap.get("number").toString())){
                double d = Double.parseDouble(todayMap.get("activeElectric").toString());
                DeviceNormalLogElectricEnergy deviceNormalLogElectricEnergy = new DeviceNormalLogElectricEnergy();
                deviceNormalLogElectricEnergy.setNumber(todayMap.get("number").toString());
                deviceNormalLogElectricEnergy.setBusinessId(todayMap.get("businessId").toString());
                deviceNormalLogElectricEnergy.setBusinessName(todayMap.get("businessName").toString());
                deviceNormalLogElectricEnergy.setActiveElectric(Double.valueOf(String.format("%.2f",d)));
                deviceNormalLogElectricEnergy.setDateTime(date);
                electricEnergyList.add(deviceNormalLogElectricEnergy);
                numberList.add(todayMap.get("number").toString());
            }
        }
        deviceNormalLogMapper.insertElectricEnergyListZB(electricEnergyList);
    }
    //充电桩-电能
    public void chargingStationEnergy(){
        String oneTableName = "device_normal_log_charging_station_"+DayUtil.getToday();
        String twoTableName = "device_normal_log_charging_station_"+DayUtil.getYestoday().replace("-","");
        //今天数据
        List<Map<String, Object>> todayList = deviceNormalLogMapper.getElectricEnergyList(oneTableName);
        //昨天数据
        List<Map<String, Object>> yesterdayList = deviceNormalLogMapper.getElectricEnergyList(twoTableName);
        //组装电数据
        List<DeviceNormalLogElectricEnergy> electricEnergyList = new ArrayList<>();
        //已添加的设备
        List<String> numberList = new ArrayList<>();
        //时间
        Date date = new Date();
        for (Map<String, Object> todayMap : todayList){
            for (Map<String, Object> yesterdayMap : yesterdayList){
                if (todayMap.get("number").equals(yesterdayMap.get("number"))){
                    double d = Double.parseDouble(todayMap.get("activeElectric").toString())-Double.parseDouble(yesterdayMap.get("activeElectric").toString());
                    DeviceNormalLogElectricEnergy deviceNormalLogElectricEnergy = new DeviceNormalLogElectricEnergy();
                    deviceNormalLogElectricEnergy.setNumber(todayMap.get("number").toString());
                    deviceNormalLogElectricEnergy.setBusinessId(todayMap.get("businessId").toString());
                    deviceNormalLogElectricEnergy.setBusinessName(todayMap.get("businessName").toString());
                    deviceNormalLogElectricEnergy.setActiveElectric(Double.valueOf(String.format("%.2f",d)));
                    deviceNormalLogElectricEnergy.setDateTime(date);
                    electricEnergyList.add(deviceNormalLogElectricEnergy);
                    numberList.add(todayMap.get("number").toString());
                }
            }
            if (!numberList.contains(todayMap.get("number").toString())){
                double d = Double.parseDouble(todayMap.get("activeElectric").toString());
                DeviceNormalLogElectricEnergy deviceNormalLogElectricEnergy = new DeviceNormalLogElectricEnergy();
                deviceNormalLogElectricEnergy.setNumber(todayMap.get("number").toString());
                deviceNormalLogElectricEnergy.setBusinessId(todayMap.get("businessId").toString());
                deviceNormalLogElectricEnergy.setBusinessName(todayMap.get("businessName").toString());
                deviceNormalLogElectricEnergy.setActiveElectric(Double.valueOf(String.format("%.2f",d)));
                deviceNormalLogElectricEnergy.setDateTime(date);
                electricEnergyList.add(deviceNormalLogElectricEnergy);
                numberList.add(todayMap.get("number").toString());
            }
        }
        deviceNormalLogMapper.insertElectricEnergyListZB(electricEnergyList);
    }
    /**
     * @Author YangYu
     * @Description 每天0点清空短信/语音当天的已使用条数
     * @Date Created in 10:14 2023/5/29
     */

    @Scheduled(cron="0 0 0 * * ?")
    public void updateSmsVoiceMargin(){
        deviceNormalLogMapper.updateSmsVoiceMargin();
    }
}
